package com.nononsenseapps.feeder.util

import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class HtmlUtilsKtTest {
    @Test
    fun findsImageInSingleLine() {
        val text = "<summary type=\"html\">&lt;img src=\"https://imgs.xkcd.com/comics/interstellar_asteroid.png\" title=\"Every time we detect an asteroid from outside the Solar System, we should immediately launch a mission to fling one of our asteroids back in the direction it came from.\" alt=\"Every time we detect an asteroid from outside the Solar System, we should immediately launch a mission to fling one of our asteroids back in the direction it came from.\" /&gt;</summary>"
        assertEquals("https://imgs.xkcd.com/comics/interstellar_asteroid.png",
                naiveFindImageLink(text))
    }

    @Test
    fun findsImageInSingleLineSrcFarFromImg() {
        val text = "<summary type=\"html\">&lt;img title=\"Every time we detect an asteroid from outside the Solar System, we should immediately launch a mission to fling one of our asteroids back in the direction it came from.\" alt=\"Every time we detect an asteroid from outside the Solar System, we should immediately launch a mission to fling one of our asteroids back in the direction it came from.\" src=\"https://imgs.xkcd.com/comics/interstellar_asteroid.png\" /&gt;</summary>"
        assertEquals("https://imgs.xkcd.com/comics/interstellar_asteroid.png",
                naiveFindImageLink(text))
    }

    @Test
    fun findsImageCount() {
        assertEquals(2, naiveFindImageLinkCount(TEST_ITEM_HTML_STR))
    }

    @Test
    fun findsImageList() {
        //assertEquals(2, naiveFindImageLinkList(TEST_ITEM_HTML_STR)?.size)
        naiveFindImageLinkList(TEST_ITEM_HTML_STR)?.forEach { println(it) }
    }

    @Test
    fun returnsNullWhenNoImageLink() {
        val text = "<summary type=\"html\">&lt;img title=\"Every time we detect an asteroid from outside the Solar System, we should immediately launch a mission to fling one of our asteroids back in the direction it came from.\" alt=\"Every time we detect an asteroid from outside the Solar System, we should immediately launch a mission to fling one of our asteroids back in the direction it came from.\" /&gt;</summary>"
        assertNull(naiveFindImageLink(text))
    }

    @Test
    fun findsUrlLink() {
        println(naiveFindUrlLink(TEST_ITEM_STR))
        assertEquals("http://117.169.124.37:6610/ysten-businessmobile/live/fhchinese/1.m3u8", naiveFindUrlLink(TEST_ITEM_STR))
    }

    @Test
    fun returnsNullForNull() {
        assertNull(naiveFindImageLink(null))
    }

    @Test
    fun htmlUnescapeTest(){
        val text = """<string name="score">Score : &#160;</string>
            | 
|            ======》 这样就能显示出冒号后面的 &#34; 或 &quot;空格""".trimMargin()
        assertEquals("Score : ======》 这样就能显示出冒号后面的  或 空格", htmlRegex.replace(text,""))
    }
}
